package org.wmq.skyserver.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.wmq.skycommon.pojo.entity.Order;
import org.wmq.skyserver.mapper.OrderMapper;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class OrderTask {
    private final OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder(){
        List<Order> orders = this.orderMapper.getByStatusAndOrderTime(1, LocalDateTime.now().minusMinutes(15L));
        if (orders == null || orders.size() == 0) {
            return;
        }
        for (Order order : orders) {
            order.setDeliverStatus(0);
            this.orderMapper.update(order);
        }
    }

    /**
     * 处理一直处于派送中的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrder(){
        log.info("处于一直处于派送中的订单：{}",LocalDateTime.now());
        List<Order> orders = this.orderMapper.getByStatusAndOrderTime(1, LocalDateTime.now().minusHours(1));
        if (orders == null || orders.size() == 0) {
            return;
        }
        for (Order order : orders) {
            order.setDeliverStatus(1);
            this.orderMapper.update(order);
        }
    }
}
